package com.xiyou.luckdraw.web.controller;

import com.xiyou.luckdraw.api.dto.answer.*;
import com.xiyou.luckdraw.api.dto.user.UpdateUserInfoResponse;
import com.xiyou.luckdraw.api.service.IAnswerBankService;
import com.xiyou.luckdraw.common.constants.ResultCodeConstants;
import com.xiyou.luckdraw.common.result.ResponseData;
import com.xiyou.luckdraw.common.result.ResponseUtils;
import com.xiyou.luckdraw.common.utils.ExportUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName: AnswerBankController
 * @Description: 题库管理
 * @author: limingxing
 * @Date: 2019-10-09 11:05
 */
@RestController
@RequestMapping("/answer")
@Api(value = "AnswerBankController", tags = {"题库管理接口"})
public class AnswerBankController {

    @Autowired
    private IAnswerBankService answerBankService;

    @PostMapping("/save")
    @ApiOperation("添加答题")
    public ResponseData save(@RequestBody AddAnswerBankRequest request) {
        AddAnswerBankResponse response = answerBankService.createAnswer(request);
        if (response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtils().setData("SUCCESS");
        }
        return new ResponseUtils().setData(response.getMsg());
    }

    @ApiOperation("屏蔽答题")
    @PostMapping("/updateByAnswerId")
    public ResponseData deleteByAnswerId(@RequestBody DeleteAnswerRequest request) {
        DeleteAnswerResponse response = answerBankService.updateByAnswerId(request);
        if (response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtils().setData(response);
        }
        return new ResponseUtils().setData(response.getMsg());
    }

    @ApiOperation("更新题目")
    @PostMapping("/updateAnswer")
    public ResponseData updateAnswer(@RequestBody UpdateAnswerRequest request) {
        UpdateAnswerResponse response = answerBankService.updateAnswer(request);
        if (response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtils().setData(response);
        }
        return new ResponseUtils().setData(response.getMsg());
    }


    @PostMapping("/list")
    @ApiOperation("答题列表")
    public ResponseData<AnswerBankListResponse> list(@RequestBody AnswerBankListRequest request) {
        AnswerBankListResponse response = answerBankService.getAnswerList(request);
        if (response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtils().setData(response);
        }
        return new ResponseUtils().setData(response.getMsg());
    }

    @PostMapping("/readExcel")
    @ApiOperation("Excel导入题目")
    public ResponseData<ExcelResponse> readExcelFile(@RequestParam("file") MultipartFile multipartFile) {
        ExcelResponse response = answerBankService.readExcelFile(multipartFile);
        if (response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtils().setData(response);
        }
        return new ResponseUtils().setData(response.getMsg());
    }

    @ApiOperation(value = "题目Excel导出")
    @GetMapping("/downloadExcel")
    public ResponseData downloadExcel(HttpServletRequest request, HttpServletResponse response) {
        List list = answerBankService.downloadAnswerExcel(request);
        String title = "题目信息";
        String[] cellValues = {"ID", "题目", "选项A", "选项B", "选项C", "选项D", "选项E", "正确答案", "状态", "等级", " 创建时间"};//Excel表头
        String[] titlename = {"id", "answerTitle", "A", "B", "C", "D", "E", "correctAnswer", "status", "level", "createTime"};//对应数据字段
        Integer[] columnWidths = {20, 40, 25, 25, 25, 25, 25, 25, 20, 25, 20};//Excel列宽度
        if (null != list) {
            ExportUtils.createExcel(request, response, title, columnWidths, cellValues, list, titlename);
        }
        return new ResponseUtils().setData("OK");
    }

    @ApiOperation(value = "列表保存题目")
    @PostMapping("/saveList")
    public ResponseData saveList(@RequestBody ExcelAnswerListRequest request) {
        ExcelResponse response = answerBankService.saveList(request);
        if (response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtils().setData(response);
        }
        return new ResponseUtils().setData(response.getMsg());
    }

}
